package com.salon.client.widgets;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.IncomeBean;
import com.salon.client.beans.PriceBean;
import com.salon.client.tabItems.IncomeTabItemPanel;
import com.salon.client.tabItems.PriceTabItemPanel;

public class PriceDialog extends Dialog{
	protected TextField<String> name = new TextField<String>();
	protected NumberField cost = new NumberField();
	private PriceTabItemPanel parentPanel;
	
	public PriceDialog(PriceTabItemPanel panel) {
		parentPanel = panel;
		setHeading("Добавление услуги");
		FormLayout layout = new FormLayout();
	    layout.setLabelWidth(90);
	    layout.setDefaultWidth(155);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.CENTER);
	    setButtons(Dialog.OKCANCEL);
	    
	    setIcon(IconHelper.createStyle("user"));
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	    setWidth(300);
	    setResizable(false);
	    
	    name.setFieldLabel("Название");
	    add(name);
	    
	    cost.setFieldLabel("Стоимость");
	    add(cost);
	    
	    Button ok = getButtonById(Dialog.OK);
		ok.setText("ОК");
		ok.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if (name.getValue() == null) {
					Info.display("Введите наименование", "услуги");
					name.focus();
					return;
				}
				
				if (cost.getValue() == null) {
					Info.display("Введите цену", "услуги");
					cost.focus();
					return;
				}
				
				else {
					PriceBean bean = new PriceBean();
					bean.setName(name.getValue());
					bean.setCost(cost.getValue().floatValue());
					
					((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
					addPrice(bean, new AsyncCallback<Void>() {

						public void onFailure(Throwable caught) {
							Info.display("Не удалось добавить", "услугу");
						}
	
						public void onSuccess(Void result) {
							Info.display("Добавление услуги", "успешно");
							PriceDialog.this.hide();
							PriceDialog.this.parentPanel.refreshStore();
						}
				});
				}
			}
			
		});
		
	    Button cancel = getButtonById(Dialog.CANCEL);
	    cancel.setText("Отмена");
	    cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				PriceDialog.this.hide();
				
			}
	    });
	}
}
